<?php

class BaseController extends CController {

    public $bannersOff = false;
    public $logoMenu;
    public $allCats;
    public $options;
    public $countNewReviews;
    public $mainMenu;
    public $brending;
    public $countNewMessages;
    public $countNewComments;
    public $rotationBanners;
        
    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        $controllerId = $this->getId();
        if (!$controllerId) return array();
        $acl = Yii::app()->params['acl'];
        if (!$acl) return array();
        if (isset($acl[$controllerId])) 
            return $acl[$controllerId];
        return array();
    }
    
    protected function beforeAction($action) {
        if(!parent::beforeAction($action)) return false;
        if(Yii::app()->controller->action->id != 'logout') {
            $isIpInBaneList = Yii::app()->db->createCommand('SELECT 1 FROM `banned_ip` where `ip`="'.CHttpRequest::getUserHostAddress().'"')->query()->read();
            if($isIpInBaneList) {
                $this->render('//partials/banPage');
                return false;
            }
        }
        return true;
    }

    public function init() {
        Yii::app()->user->setState('xuploadFiles', array());
        Yii::app()->bootstrap->registerCoreCss();
        Yii::app()->bootstrap->registerYiiCss();
        Yii::app()->bootstrap->registerCoreScripts();
    }
    
    public function beforeRender() {
        $this->allCats = Yii::app()->db->cache(86400, new CacheTaggedDependency(array('partners_cat')))->createCommand(array('select'=>'name, catId, parentId', 'from'=>'partners_cat'))->queryAll();
        $this->logoMenu = Yii::app()->db->cache(86400, new CacheTaggedDependency(array('logo_menu')))->createCommand(array('select'=>'*', 'from'=>'logo_menu'))->queryAll();
        $this->mainMenu = Yii::app()->db->cache(86400, new CacheTaggedDependency(array('main_menu')))->createCommand(array('select'=>'*', 'from'=>'main_menu'))->queryAll();
        if(!Yii::app()->user->isGuest) {
            $this->countNewMessages = Yii::app()->db->createCommand()->select('count(*) as count')->from('messages_dialogs')->where('user1New IN('.Yii::app()->user->getId().') OR user2New IN('.Yii::app()->user->getId().')')->queryRow();
            $this->countNewComments = Yii::app()->db->createCommand()->select('count(*) as count')->from('reviews_comments')->where('`isNew`='.Yii::app()->user->getId())->queryRow();   
//            Yii::app()->db->createCommand("INSERT IGNORE INTO `user_activity` (userId, time) VALUES (".Yii::app()->user->getId().", NOW()) ON DUPLICATE KEY UPDATE `time`=NOW()")->execute(); 
        }
        if(Yii::app()->user->checkAccess('redactor'))
            $this->countNewReviews = Yii::app()->db->createCommand()->select('count(*)')->from('reviews')->where('`status`="Новый"')->queryRow();
        $data = Yii::app()->db->createCommand()->select('*')->from('banners_big')->queryAll();
        foreach ($data as $k=>$v) {
            if($v['type'] == '720х90 px') {
                $this->rotationBanners['720х90 px'] = $data[$k];
            }
            elseif($v['type'] == '1000x60 px') {
                $this->rotationBanners['1000x60 px'] = $data[$k]; 
            }
        }
        return true;
    }
    
    protected function brending($type, $targetId=0, $parentId=0) {
        if($type == 'cat') {
            if($parentId) {
                $this->brending = Yii::app()->db->cache(86400, new CacheTaggedDependency(array('brending')))->createCommand()->select('*')->from('brending')->where('`type`="'.$type.'" and (`typeId`='.$targetId.' or `typeId`='.$parentId.')')->queryAll();
                if(count($this->brending) == 2) {
                    if($this->brending[0]['typeId'] != $targetId) array_splice($this->brending, 0, 1);
                    else array_splice($this->brending, 1, 1);
                }
            } else $this->brending = Yii::app()->db->cache(86400, new CacheTaggedDependency(array('brending')))->createCommand()->select('*')->from('brending')->where('`type`="'.$type.'" and `typeId`='.$targetId)->queryAll();
        } elseif($type == 'partner') {
            $this->brending = Yii::app()->db->cache(86400, new CacheTaggedDependency(array('brending')))->createCommand()->select('*')->from('brending')->where('(`type`="'.$type.'" and `typeId`='.$targetId.') or (`type`="cat" and `typeId`='.$parentId.')')->queryAll();
            if(count($this->brending) == 2) {
                if($this->brending[0]['typeId'] != $targetId) array_splice($this->brending, 0, 1);
                else array_splice($this->brending, 1, 1);
            }   
        } else $this->brending = Yii::app()->db->cache(86400, new CacheTaggedDependency(array('brending')))->createCommand()->select('*')->from('brending')->where('`type`="'.$type.'"')->queryAll();
    }
}